package com.example.glaciermessage.controller;

import com.codingfuture.glaciercommon.util.Result;
import com.example.glaciermessage.service.SmsService;
import com.example.glaciermessage.util.SMS;
import com.example.glaciermessage.vo.SmsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class SmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private SMS sms;

    //    发送预警短信
    @GetMapping("/sms")
//    public Result<Object> sms(@RequestBody List<SmsVO> vs) throws Exception {
    public Result<Object> sms() throws Exception {

        SmsVO smsVO = new SmsVO(1L,"lyx",10L,10L,10L);
        ArrayList<SmsVO> vs = new ArrayList<>();
        vs.add(smsVO);
//            System.out.println(vs);
        if(vs.isEmpty()){
            return Result.error(3080,"发送");
        }
        List<SmsVO> SmsVO = smsService.findViewStorealert();
        if(SmsVO.size() <= 0){
            return Result.fail(2000,"目前仓库充足，无需补货");
        }
//        String phoneNumber = "13795190601";//xw
//        String phoneNumber = "15142638023";
        String phoneNumber = "15940419239";

        String name = "lyx";
        String store = "" + vs.get(0).getId() + "等";
//        System.out.println(store);
        String goods = "" + vs.get(0).getName() + "等";
//        String templateParam = String.format("{\"name\":\"%s\"}",name);
        String templateParam = String.format("{\"name\":\"%s\",\"store\":\"%s\",\"goods\":\"%s\"}",name,store,goods);

        String s = sms.sendMsg(phoneNumber, templateParam);
        System.out.println(s);

        return "OK".equals(s)? Result.success():Result.error(3080,"发送失败");
    }

//    测试
    @GetMapping("/test")
    public String test() throws Exception {
        String phoneNumber = "15940419239";
        String s = sms.sendMsg(phoneNumber,null);
        System.out.println(s);
        return s;
    }
}
